package cn.itcast.tags.spark.hbase

import org.apache.hadoop.hbase.filter.CompareFilter
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp

import scala.util.matching.Regex

case class Condition(
                    field:String,
                    compare:CompareFilter.CompareOp,
                    value:String
                    )
object Condition{
  val FULL_REGEX: Regex = "(.*?)\\[(.*?)\\](.*+)".r

  def  parseCondition(filterCondition:String):Condition={
    val optionMatch: Option[Regex.Match] = FULL_REGEX.findFirstMatchIn(filterCondition)
    val matchValue: Regex.Match = optionMatch.get



    val compareOp: CompareOp = matchValue.group(2).toLowerCase() match {
      case "eq" => CompareOp.EQUAL
      case "ne" => CompareOp.NOT_EQUAL
      case "gt" => CompareOp.GREATER
      case "lt" => CompareOp.LESS
      case "ge" => CompareOp.GREATER_OR_EQUAL
      case "le" => CompareOp.LESS_OR_EQUAL
    }

    Condition(
      matchValue.group(1),
      compareOp,
      matchValue.group(3)
    )
  }

}